package com.atguigu.redislock.mylock;

import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.locks.Lock;

/**
 * @auther zzyy
 * @create 2022-10-23 22:40
 */
@Component
public class DistributedLockFactory
{
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private String lockName;

    private String uuidValue;
    public DistributedLockFactory()
    {
        this.uuidValue = IdUtil.simpleUUID();//UUID
    }


    public Lock getDistributedLock(String lockType)
    {
        if (lockType == null) return null;

        if(lockType.equalsIgnoreCase("REDIS")) {
            /*lockName = "zzyyRedisLock";
            return new RedisDistributedLock(stringRedisTemplate,lockName);*/
            lockName = "zzyyRedisLock";
            return new RedisDistributedLock(stringRedisTemplate,lockName,uuidValue);
        }else if(lockType.equalsIgnoreCase("ZOOKEEPER")){
            lockName = "zzyyZookeeperLock";
            return new ZookeeperDistributedLock();
        }else{
            //......
        }
        return null;
    }
}
